home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / ispell40.lha / ispell-4.0 / nextprime.c < prev    next >
C/C++ Source or Header  |  1993-04-15  |  1KB  |  58 lines

  1. /* Copyright (C) 1990, 1993 Free Software Foundation, Inc.
  2.  
  3.    This file is part of GNU ISPELL.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <stdio.h>
  20.  
  21. unsigned short
  22. nextprime (x)
  23.   unsigned short x;
  24. {
  25.   unsigned short half;
  26.   unsigned short y;
  27.  
  28.   if (x == 2)
  29.     return (2);
  30.  
  31.   if (x % 2 == 0)
  32.     x++;
  33.  
  34.   while (1)
  35.     {
  36.       half = x / 2;
  37.       for (y = 3; y < half; y += 2)
  38.     if (x % y == 0)
  39.       goto next;
  40.       return (x);
  41.     next:
  42.       x += 2;
  43.       if (x == 1)
  44.     return (0);
  45.     }
  46. }
  47.  
  48. #if 0
  49. main ()
  50. {
  51.   char buf[100];
  52.  
  53.   while (gets (buf) != NULL)
  54.     (void) printf ("%u\n", nextprime (atoi (buf)));
  55. }
  56.  
  57. #endif
  58.